import api from './api';
import { UserRole } from '@/types';

// 学生类型定义
export interface Student {
  id: number;
  studentNumber?: string;
  name: string;
  gender: string;
  birthDate?: string;
  classInfo?: {
    id: number;
    name: string;
    grade: string;
    teacher?: {
      id: number;
      name: string;
      username: string;
      phone?: string;
      email?: string;
    }
  };
  parent?: {
    id: number;
    username: string;
    name: string;
    phone?: string;
    email?: string;
  };
  phone?: string;
  address?: string;
  remarks?: string;
  enabled?: boolean;
  createTime: string;
  updateTime?: string;
}



// 分页响应接口
export interface PageResponse<T> {
  content: T[];
  pageable: {
    pageNumber: number;
    pageSize: number;
    sort: {
      empty: boolean;
      sorted: boolean;
      unsorted: boolean;
    };
    offset: number;
    paged: boolean;
    unpaged: boolean;
  };
  last: boolean;
  totalElements: number;
  totalPages: number;
  size: number;
  number: number;
  sort: {
    empty: boolean;
    sorted: boolean;
    unsorted: boolean;
  };
  numberOfElements: number;
  first: boolean;
  empty: boolean;
}

// 查询参数类型
export interface StudentQueryParams {
  page?: number;
  size?: number;
  classId?: number;
  parentId?: number;
  keyword?: string;
}

// 创建学生参数类型
export interface CreateStudentParams {
  name: string;
  gender: string;
  birthday?: string;
  classId?: number;
  parentId?: number;
  enabled: boolean;
}

// 更新学生参数类型
export interface UpdateStudentParams {
  name?: string;
  gender?: string;
  birthday?: string;
  classId?: number;
  parentId?: number;
  enabled?: boolean;
}

// API响应格式
interface ApiResponse<T> {
  data: T;
  message: string;
  status: string;
}

/**
 * 获取学生列表
 * @param params 查询参数
 * @returns 学生列表分页数据
 */
export const getStudents = async (params: StudentQueryParams = {}): Promise<PageResponse<Student>> => {
  try {
    const { page = 0, size = 10, classId, parentId, keyword } = params;
    
    // 构建查询参数
    const queryParams: Record<string, any> = { page, size };
    
    // 添加可选查询参数
    if (keyword) queryParams.keyword = keyword;
    if (classId) queryParams.classId = classId;
    if (parentId) queryParams.parentId = parentId;
    
    console.log('获取学生列表查询参数:', queryParams);
    
    const { data } = await api.get<PageResponse<Student>>('/admin/students', {
      params: queryParams
    });
    console.log('获取学生列表数据:', data);
    return data;
  } catch (error) {
    console.error('获取学生列表失败:', error);
    throw error;
  }
};

/**
 * 获取学生详情
 * @param id 学生ID
 * @returns 学生详细信息
 */
export const getStudent = async (id: number): Promise<Student> => {
  try {
    const { data } = await api.get<Student>(`/admin/students/${id}`);
    console.log('获取学生详情数据:', data);
    return data;
  } catch (error) {
    console.error('获取学生详情失败:', error);
    throw error;
  }
};

/**
 * 创建学生
 * @param params 学生信息
 * @returns 创建的学生信息
 */
export const createStudent = async (params: CreateStudentParams): Promise<Student> => {
  try {
    const { data } = await api.post<Student>('/admin/students', params);
    console.log('创建学生结果:', data);
    return data;
  } catch (error) {
    console.error('创建学生失败:', error);
    throw error;
  }
};

/**
 * 更新学生信息
 * @param id 学生ID
 * @param params 更新的学生信息
 * @returns 更新后的学生信息
 */
export const updateStudent = async (id: number, params: UpdateStudentParams): Promise<Student> => {
  try {
    const { data } = await api.put<Student>(`/admin/students/${id}`, params);
    console.log('更新学生结果:', data);
    return data;
  } catch (error) {
    console.error('更新学生失败:', error);
    throw error;
  }
};

/**
 * 删除学生
 * @param id 学生ID
 * @returns 是否成功
 */
export const deleteStudent = async (id: number): Promise<boolean> => {
  try {
    await api.delete(`/admin/students/${id}`);
    console.log('删除学生成功');
    return true;
  } catch (error) {
    console.error('删除学生失败:', error);
    throw error;
  }
};

